package handle;

import common.ClientHandle;
import common.Server;

/**
 * 进行推模型.发送通知到注册了的client.
 *
 * @author guanweichen
 * @version Jan 18, 2011
 */
public class LocalUpFileNotifyHandle extends ClientHandle {

	public LocalUpFileNotifyHandle(Server server) {
		this.server = server;
	}
	@Override
	public void run() {
		
//		LocalClient client = new LocalClient("127.0.0.1", 8889);
//		LocalFileUpNotifyQueue fileUpNotifyQueue = new LocalFileUpNotifyQueue(client, server.getListShareFile().get(0));
//		server.getListNotifyQueue().add(fileUpNotifyQueue);
		
		/* wait 实现,文件更新通知
		 * while (server.isStarted()) {
			try {
				sleep(Integer.parseInt(Server.rb.getString("time_span")));
			} catch (InterruptedException e) {
				e.printStackTrace();
			}// 睡眠十分钟,进行文件更新通知.
			
			List<FileUpNotifyQueue> errorList = new ArrayList<FileUpNotifyQueue>();
			for (FileUpNotifyQueue notifyQueue : server.getListNotifyQueue()) {
				File file = new File(notifyQueue.getShareFileModel().getServerFilePath());
				// 文件更新cmd
				FileUpCommand command = new FileUpCommand(notifyQueue.getShareFileModel().getFileName());
				command.setFile(file);
				try {
					Socket socket = new Socket(notifyQueue.getClient().getAddress().replace("/", ""), notifyQueue.getClient().getPort());
					Command retMess =(Command) sendCmd(command, socket);
					System.out.println("send update file to client '" + notifyQueue.getClient().getAddress() + "' is : " + retMess.getCommand());
				} catch (Exception e) {
					System.out.println("send update file to client '" + notifyQueue.getClient().getAddress() + "' is : " + Command.FAILED);
					System.out.println("remove this client '" + notifyQueue.getClient().getAddress() + ", " + notifyQueue.getClient().getPort() + "' to notity queue ");
					errorList.add(notifyQueue);
					e.printStackTrace();
					continue;
				}
			}
			// 清除发生错误的通知队列中成员.
			System.out.println("Notify LIST:" + server.getListNotifyQueue().size());
			System.out.println("Notify ERROR LIST: " + errorList.size());
			server.getListNotifyQueue().removeAll(errorList);
			errorList.clear();
		}*/
	}

}
